package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.res.Configuration;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;

import java.util.Locale;

public class UnableToAccessServerDialogFragment extends AbsDialogFragment {
    public static final String NETWORK_WARNING = "NetworkWarning";
    public static final String WIFI_WARNING = "WifiWarning";

    int mEntranceId;
    Activity mActivity;

    public static UnableToAccessServerDialogFragment getInstance(int entranceId, Activity activity) {
        UnableToAccessServerDialogFragment dialog = new UnableToAccessServerDialogFragment();
        dialog.initValue(entranceId, activity);
        return dialog;
    }

    public UnableToAccessServerDialogFragment() {
    }

    private void initValue(int entranceId, Activity activity) {
        mEntranceId = entranceId;
        mActivity = activity;
    }

    @Override
    protected Dialog _createDialog() {
        AlertDialog.Builder builder = null;
        switch (mEntranceId) {
            case R.string.unable_to_connect_network:
                builder = createNoNetworkDialog();
                break;
            case R.string.unable_to_connect_wifi:
                builder = createNoWiFiDialog();
                break;
        }

        if (builder != null) {
            AlertDialog dialog = builder.create();
            dialog.setTitle(R.string.unable_to_access_server);
            return dialog;
        }

        return null;
    }

    private AlertDialog.Builder createNoNetworkDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.mobile_data_disabled);
        builder.setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                dismissDialog();
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.NETWORK_NOT_AVAILABLE_POPUP, SamsungAnalyticsLog.Event.OK, null);
            }
        });

        return builder;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mNavigationManager != null) {
            NavigationInfo curInfo = mNavigationManager.getCurInfo();
            if (curInfo != null) {
                if (AppFeatures.isNote() && (curInfo.getStorageType() != FileRecord.StorageType.Cloud)) {
                    dismissDialog();
                }
            }
        }
    }

    private AlertDialog.Builder createNoWiFiDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.wifi_disabled);
        builder.setNegativeButton(R.string.capital_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                dismissDialog();
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.WIFI_NOT_AVAILABLE_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
            }
        });

        builder.setPositiveButton(getString(R.string.capital_settings).toUpperCase(Locale.getDefault()), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                MyFilesFacade.goSetting(mNavigationManager.getProcessId(), mActivity);
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.WIFI_NOT_AVAILABLE_POPUP, SamsungAnalyticsLog.Event.SETTINGS, null);
            }
        });

        return builder;
    }

    private void dismissDialog() {
        if (isResumed()) {
            dismissAllowingStateLoss();
        }
    }

    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }
}
